﻿namespace HRM.CORE.CLASS
{
    using DevExpress.XtraPrinting;
    using DevExpress.XtraReports.UI;
    using System; using Extra.ERP;
    using System.Collections;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms; using Common;

    public class CreateFooterReport
    {
        private static Font m_FontFullName;
        private static Font m_FontPosition;
        private static Font m_FontRequire;
        private static float m_HeightFooter;
        private static bool m_IsShowFullName;
        private static float m_SpaceAbove;
        private static string xmlFormatOptionPath = (Application.StartupPath + @"\Layout\footerReportFormatOption.xml");
        private static string xmlListOptionPath = (Application.StartupPath + @"\Layout\footerReportListOption.xml");

        private static DataTable CreateDataTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Position", typeof(string));
            table.Columns.Add("FullName", typeof(string));
            table.Columns.Add("Require", typeof(string));
            table.Columns.Add("IsTimekeeper", typeof(bool));
            table.Columns.Add("IsSalary", typeof(bool));
            table.Columns.Add("IsOther", typeof(bool));
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(xmlListOptionPath);
                foreach (DataRow row in set.Tables[0].Rows)
                {
                    table.Rows.Add(new object[] { row[0].ToString(), row[1].ToString(), row[2].ToString(), bool.Parse(row[3].ToString()), bool.Parse(row[4].ToString()), bool.Parse(row[5].ToString()) });
                }
            }
            catch
            {
                table.Rows.Add(new object[] { "Người Lập", "", "(K\x00fd, ghi r\x00f5 họ t\x00ean)", true, true, true });
                table.Rows.Add(new object[] { "PT. Bộ Phận", "", "(K\x00fd, ghi r\x00f5 họ t\x00ean)", true, true, false });
                table.Rows.Add(new object[] { "Kế To\x00e1n", "", "(K\x00fd, ghi r\x00f5 họ t\x00ean)", false, true, false });
                table.Rows.Add(new object[] { "Gi\x00e1m Đốc", "", "(K\x00fd, ghi r\x00f5 họ t\x00ean)", true, true, true });
            }
            return table;
        }

        public static void CreateFooter(string Type, XRControl Band, XtraReport Report)
        {
            LoadFormatOption();
            DataTable table = CreateDataTable();
            XRTable child = new XRTable();
            XRTable table3 = new XRTable();
            XRTableRow row = new XRTableRow();
            XRTableRow row2 = new XRTableRow();
            XRTableRow row3 = new XRTableRow();
            foreach (DataRow row4 in table.Rows)
            {
                if (bool.Parse(row4[Type].ToString()))
                {
                    XRTableCell cell = new XRTableCell {
                        Text = row4["Position"].ToString(),
                        TextAlignment = TextAlignment.TopCenter,
                        Font = m_FontPosition
                    };
                    row.Cells.Add(cell);
                    XRTableCell cell2 = new XRTableCell {
                        Text = row4["Require"].ToString(),
                        TextAlignment = TextAlignment.TopCenter,
                        Font = m_FontRequire
                    };
                    row2.Cells.Add(cell2);
                    XRTableCell cell3 = new XRTableCell {
                        Text = row4["FullName"].ToString(),
                        TextAlignment = TextAlignment.MiddleCenter,
                        Font = m_FontFullName
                    };
                    row3.Cells.Add(cell3);
                }
            }
            child.Rows.Add(row);
            child.Rows.Add(row2);
            table3.Rows.Add(row3);
            child.WidthF = Report.PageWidth - (Report.Margins.Left + Report.Margins.Right);
            child.TopF = GetMaximumTopF(Band) + m_SpaceAbove;
            table3.WidthF = child.WidthF;
            table3.TopF = child.TopF + m_HeightFooter;
            Band.Controls.Add(child);
            if (m_IsShowFullName)
            {
                Band.Controls.Add(table3);
            }
        }

        private static float GetMaximumTopF(XRControl Band)
        {
            float topF = Band.TopF;
            foreach (XRControl control in (IEnumerable) Band)
            {
                if (control.TopF > topF)
                {
                    topF = control.TopF;
                }
            }
            return topF;
        }

        private static void LoadFormatOption()
        {
            FontConverter converter = new FontConverter();
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(xmlFormatOptionPath);
                m_SpaceAbove = float.Parse(set.Tables[0].Rows[0]["SpaceAbove"].ToString());
                m_HeightFooter = float.Parse(set.Tables[0].Rows[0]["HeightFooter"].ToString());
                m_IsShowFullName = bool.Parse(set.Tables[0].Rows[0]["IsShowFullName"].ToString());
                m_FontPosition = converter.ConvertFromString(set.Tables[0].Rows[0]["FontPosition"].ToString()) as Font;
                m_FontFullName = converter.ConvertFromString(set.Tables[0].Rows[0]["FontFullName"].ToString()) as Font;
                m_FontRequire = converter.ConvertFromString(set.Tables[0].Rows[0]["FontRequire"].ToString()) as Font;
            }
            catch
            {
                m_SpaceAbove = 40f;
                m_HeightFooter = 70f;
                m_IsShowFullName = true;
                m_FontPosition = converter.ConvertFromString("Times New Roman, 11,25pt, style=Bold") as Font;
                m_FontFullName = converter.ConvertFromString("Times New Roman, 11,25pt, style=Regular") as Font;
                m_FontRequire = converter.ConvertFromString("Times New Roman, 9,25pt, style=Italic") as Font;
            }
        }
    }
}

